//
//  UNAccountVC.swift
//  UNHimalayas
//
//  Created by MaHaoZhe on 2019/11/21.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class UNAccountVC: UIViewController {
    
    var scroolView : UIScrollView!
    var contentView : UIView!
    var myHeaderView : MyHeaderView!
    var myContentView : MyContentView!
    var myServeView : MyServeView!
    var myToolView : MyToolView!
    var myOthersServeView : MyOthersServeView!
    var myBusinessCooperationView : MyBusinessCooperationView!
    var copyrightBtn : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.edgesForExtendedLayout = []
        self.navigationItem.title = ""
        self.view.backgroundColor = RGBA(240, g: 240, b: 240, a: 1.0);
        
        initAction()
        initScroolView()
        initContentView()
        initBgView()
        initMyHeaderView()
        initMyContentView()
        initMyServeView()
        initMyToolView()
        initMyOthersServeView()
        initMyBusinessCooperationView()
        initCopyrightButton()
        
        self.view.layoutIfNeeded()
    }
    
    func initAction() -> Void {
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "live_person_ic_msg_58C89562F58FD276F592420068DB8C09_0"), for: UIControl.State.normal)
        leftBtn.setImage(UIImage(named: "live_person_ic_msg_58C89562F58FD276F592420068DB8C09_0"), for: UIControl.State.highlighted)
           
        leftBtn.addTarget(self, action: #selector(UNAccountVC.leftAction), for: UIControl.Event.touchUpInside)
        // button自适应大小
        leftBtn.sizeToFit()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView:leftBtn)
        // 2.定义右边的按钮
        let rightBtn = UIButton()
        rightBtn.setImage(UIImage(named: "XMRecorder_rec_abc_ic_set_58C89562F58FD276F592420068DB8C09_0"), for: UIControl.State.normal)
        rightBtn.setImage(UIImage(named: "XMRecorder_rec_abc_ic_set_58C89562F58FD276F592420068DB8C09_0"), for: UIControl.State.highlighted)
        // button自适应大小
        rightBtn.sizeToFit()
        rightBtn.addTarget(self, action: #selector(UNAccountVC.rightAction), for: UIControl.Event.touchUpInside)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView:rightBtn)
    }
    
    func initScroolView() -> Void {
        
        scroolView = UIScrollView()
        scroolView.backgroundColor = UIColor.clear
        self.view.addSubview(scroolView)
        
        scroolView.snp.makeConstraints { (make) in
            
            make.edges.equalToSuperview()
        }
    }
    
    func initContentView() -> Void {
        
        contentView = UIView()
        contentView.backgroundColor = UIColor.clear
        self.scroolView.addSubview(contentView)
        
        contentView.snp.makeConstraints { (make) in
            
            make.edges.equalToSuperview()
            make.width.equalTo(kScreenWidth)
            make.height.greaterThanOrEqualToSuperview()
        }
    }
    
    func initBgView() -> Void {
        
        let bgView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kRealWidth(50)))
        bgView.backgroundColor = UIColor.clear
        self.contentView.addSubview(bgView)
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.view.bounds
        bgView.layer.addSublayer(gradientLayer)
        gradientLayer.colors = [UIColor.white.cgColor,
                                        RGBA(240, g: 240, b: 240, a: 1.0).cgColor,]
        let gradientLocations:[NSNumber] = [0.0,1.0]
        gradientLayer.locations = gradientLocations
//        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0)
//        gradientLayer.endPoint = CGPoint.init(x: 1, y: 1)
    }
    
    func initMyHeaderView() -> Void {
        
        myHeaderView = MyHeaderView()
        myHeaderView.backgroundColor = UIColor.clear
        self.contentView.addSubview(myHeaderView)
        
        myHeaderView.snp.makeConstraints { (make) in
            
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(kRealWidth(100))
        }
    }
    
    func initMyContentView() -> Void {
        
        myContentView = MyContentView()
        myContentView.backgroundColor = UIColor.white
        myContentView.layer.cornerRadius = kRealWidth(8)
        myContentView.layer.masksToBounds = true
        self.contentView.addSubview(myContentView)
        
        myContentView.viewClicked = {(index) -> () in
            
            print(index as Any)
        }
        
        myContentView.snp.makeConstraints { (make) in
            
            make.top.equalTo(myHeaderView.snp.bottom).offset(kRealWidth(17))
            make.left.equalToSuperview().offset((kRealWidth(15)))
            make.right.equalToSuperview().offset(kRealWidth(-15))
            make.height.greaterThanOrEqualTo(kRealWidth(88))
        }
    }
    
    func initMyServeView() -> Void {
        
        myServeView = MyServeView()
        myServeView.backgroundColor = UIColor.white
        myServeView.layer.cornerRadius = kRealWidth(8)
        myServeView.layer.masksToBounds = true
        self.contentView.addSubview(myServeView)
        
        myServeView.viewClicked = {(index) -> () in
            
            print(index as Any)
            
            if index == 0 {
                
                //我的钱包
                let vc = MyWalletVC()
                self.navigationController?.pushViewController(vc, animated: true)
            }
        }
        
        myServeView.snp.makeConstraints { (make) in
            
            make.top.equalTo(myContentView.snp.bottom).offset(kRealWidth(17))
            make.left.equalToSuperview().offset((kRealWidth(15)))
            make.right.equalToSuperview().offset(kRealWidth(-15))
            make.height.greaterThanOrEqualTo(kRealWidth(88))
        }
    }
    
    func initMyToolView() -> Void {
        
        myToolView = MyToolView()
        myToolView.backgroundColor = UIColor.white
        myToolView.layer.cornerRadius = kRealWidth(8)
        myToolView.layer.masksToBounds = true
        self.contentView.addSubview(myToolView)
        
        myToolView.viewClicked = {(index) -> () in
            
            print(index as Any)
        }
        
        myToolView.snp.makeConstraints { (make) in
            
            make.top.equalTo(myServeView.snp.bottom).offset(kRealWidth(17))
            make.left.equalToSuperview().offset((kRealWidth(15)))
            make.right.equalToSuperview().offset(kRealWidth(-15))
            make.height.greaterThanOrEqualTo(kRealWidth(88))
        }
    }
    
    func initMyOthersServeView() -> Void {
        
        myOthersServeView = MyOthersServeView()
        myOthersServeView.backgroundColor = UIColor.white
        myOthersServeView.layer.cornerRadius = kRealWidth(8)
        myOthersServeView.layer.masksToBounds = true
        self.contentView.addSubview(myOthersServeView)
        
        myOthersServeView.viewClicked = {(index) -> () in
            
            print(index as Any)
        }
        
        myOthersServeView.snp.makeConstraints { (make) in
            
            make.top.equalTo(myToolView.snp.bottom).offset(kRealWidth(17))
            make.left.equalToSuperview().offset((kRealWidth(15)))
            make.right.equalToSuperview().offset(kRealWidth(-15))
            make.height.greaterThanOrEqualTo(kRealWidth(88))
        }
    }
    
    
    func initMyBusinessCooperationView() -> Void {
        
        myBusinessCooperationView = MyBusinessCooperationView()
        myBusinessCooperationView.backgroundColor = UIColor.white
        myBusinessCooperationView.layer.cornerRadius = kRealWidth(8)
        myBusinessCooperationView.layer.masksToBounds = true
        self.contentView.addSubview(myBusinessCooperationView)
        
        myBusinessCooperationView.viewClicked = {(index) -> () in
            
            print(index as Any)
        }
        
        myBusinessCooperationView.snp.makeConstraints { (make) in
            
            make.top.equalTo(myOthersServeView.snp.bottom).offset(kRealWidth(17))
            make.left.equalToSuperview().offset((kRealWidth(15)))
            make.right.equalToSuperview().offset(kRealWidth(-15))
            make.height.greaterThanOrEqualTo(kRealWidth(88))
            make.bottom.equalToSuperview().offset(kRealWidth(-20)).priority(750)
        }
    }
    
    
    func initCopyrightButton() -> Void {
        
        copyrightBtn = UIButton()
        copyrightBtn.backgroundColor = UIColor.orange
        copyrightBtn.setBackgroundImage(UIImage(named: ""), for: .normal)
        copyrightBtn.setBackgroundImage(UIImage(named: ""), for: .selected)
        copyrightBtn.setImage(UIImage(named: ""), for: .normal)
        copyrightBtn.setImage(UIImage(named: ""), for: .selected)
        copyrightBtn.layer.cornerRadius = kRealWidth(8)
        copyrightBtn.layer.masksToBounds = true
        copyrightBtn.addTarget(self, action: #selector(clickedCopyBtn(btn:)), for: .touchUpInside)
        self.contentView.addSubview(copyrightBtn)
        
        copyrightBtn.snp.makeConstraints { (make) in
            
            make.top.equalTo(myBusinessCooperationView.snp.bottom).offset(kRealWidth(17));
            make.left.equalToSuperview().offset((kRealWidth(15)))
            make.right.equalToSuperview().offset(kRealWidth(-15))
            make.height.greaterThanOrEqualTo(kRealWidth(88))
            make.bottom.equalToSuperview().offset(kRealWidth(-20)).priority(1000)
        }
    }
    
    // MARK: - 点击
    @objc func leftAction() -> () {
        
        print("点击了左边")
    }

    @objc private func rightAction(){

        print("点击了右边")
        
        let vc = SetVC()
        
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func clickedCopyBtn(btn : UIButton) -> Void {
        
        print("我要保护版权")
    }
}
